<template>


    <div class="page-box">

        <div class="page-search">

            <div>
                <label>工时类型</label>
                <el-select v-model="searchForm.WorkhoursType" placeholder="请选择工时类型">
                    <el-option label="钳工" value="钳工"></el-option>
                    <el-option label="浇筑" value="浇筑"></el-option>
                </el-select>
            </div>

            <div>
                <label>工时(小时)</label>
                <el-input v-model="searchForm.Workhours" style="width: 160px;"></el-input>
            </div>

            <div>
                <label>员工姓名</label>
                <el-input v-model="searchForm.WorkerName" style="width: 160px;"></el-input>
            </div>

            <div>
                <el-button type="primary" @click="search">查询</el-button>
                <el-button @click="reset">重置</el-button>
            </div>

        </div>


        <div class="page-section">

            <div class="page-tool">
                <el-button type="primary" plain icon="el-icon-plus" @click="add" size="small">新增</el-button>
                <el-button type="danger" plain icon="el-icon-delete" @click="dels" size="small">删除</el-button>


                <el-dropdown class="mg-l">
                    <el-button size="small" type="primary" plain>
                        <i class="el-icon-s-grid"></i> 
                        <i class="el-icon-arrow-down el-icon--right"></i>
                    </el-button>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="exports"><i class="el-icon-receiving"></i>导出</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>

            </div>
            
            <el-descriptions class="margin-top" :column="3">
                <el-descriptions-item label="总工时 (小时)">  {{WorkhoursSum}}</el-descriptions-item>
            </el-descriptions>

            <!--表单-->
            <el-table :data="tableData"  ref="dataForm"   v-loading="isLoading"  height="calc(100vh - 350px)">
                
                <el-table-column  type="selection" width="55"></el-table-column>

                <el-table-column fixed prop="WorkhoursType" label="工时类型" :show-overflow-tooltip="true"></el-table-column>

                <el-table-column fixed prop="Workhours" label="工时(小时)" :show-overflow-tooltip="true"></el-table-column>

                <el-table-column fixed prop="WorkerName" label="员工姓名" :show-overflow-tooltip="true"></el-table-column>

                <el-table-column fixed prop="WorkshopName" label="所属车间" :show-overflow-tooltip="true"></el-table-column>

                <el-table-column   label="操作" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        <el-button type="text" @click="update(scope.row)">编辑</el-button>
                        <el-button type="text" @click="del(scope.row)">删除</el-button>
                    </template>
                </el-table-column>

            </el-table>

            <Pagination @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange" :PageIndex="pageDomain.PageIndex" :PageSize="pageDomain.PageSize" :TotalCount="pageDomain.TotalCount"/>
        </div>

        <!--弹窗-->
        <el-dialog  :close-on-click-modal="false" :title="dialogDomain.title" :visible.sync="dialogDomain.dialogVisible" width="70%" @close="closeDialog">
            <ProTemporaryWorkHoursInInput :model="model" :isUpdate="isUpdate" @callback="callback" v-if="dialogDomain.dialogVisible"/>
        </el-dialog>


    </div>



</template>

<script>
//es6对象名
import ProTemporaryWorkHoursInInput from './components/ProTemporaryWorkHoursInInput.vue';
import Pagination from '@/components/Pagination.vue';
export default {
    components:{
        ProTemporaryWorkHoursInInput,
        Pagination
    },
    mounted(){
        this.dataBind();
        this.GetWorkhours();
    },
    data(){
        return{
            searchForm:{
                //搜索字段
                WorkhoursType:'',
                Workhours:'',
                WorkerName:'',
            },
            WorkhoursSum:0,

            //弹出对话框
            dialogDomain:{
                title:'新增临时工时',
                dialogVisible:false
            },

            //修改状态为否
            isUpdate:false,

            //数据模型
            model:{
                WorkhoursType:'',
                Workhours:'',
                WorkerName:'',
                WorkshopName:'',
            },

            //分页
            pageDomain:{
                PageIndex:1,
                PageSize:10,
                TotalCount:0
            },
            tableData:[],
            isLoading:false
        }
    },
    methods:{
        add(){
            this.dialogDomain.title="新增临时工时";
            this.dialogDomain.dialogVisible=true;
            this.isUpdate=false;
        },
        update(model){
            this.dialogDomain.title="编辑临时工时";
            this.dialogDomain.dialogVisible=true;
            this.isUpdate=true;

            console.log(model);
            this.model=model;
        },

        //批量删除
        dels(){
            if(this.$refs.dataForm.selection.length<=0) {
                this.showNotify('请先选择一项')
                return;
            }
            this.$confirm('确定删除选中的数据吗？这将不可恢复, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
            .then(()=>{
                //获取选中的表格数据 并筛选id
                var ids =this.$refs.dataForm.selection.map(x=>x.Id)

                //拼接参数
                let params =  new URLSearchParams();
                params.append('ids',ids.join(','))

                this.$http.delete('/api/ProTemporaryWorkHours/DelAll',{
                    data:params,
                    headers:{
                        'content-Type': 'application/x-www-form-urlencoded'
                    },
                    loading:this.$loading()
                })
                .then((res)=>{
                    this.showNotify('操作成功','success');
                    this.dataBind();
                    this.GetWorkhours();
                })
            }).catch(()=>{});
        },

        //删除单条数据
        del(model){
            this.$confirm('确定删除选中的数据吗？这将不可恢复, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
            .then(() => {
                this.$http.delete('/api/ProTemporaryWorkHours',{
                    data:{
                        id:model.Id
                    },
                    headers:{
                        'content-Type': 'application/x-www-form-urlencoded'
                    }
                })
                .then((res)=>{
                    this.showNotify('操作成功','success');
                    this.dataBind();
                    this.GetWorkhours();
                })
            }).catch(()=>{});  
        },


        //导出
        exports(){
            this.$confirm('即将导出报表，是否继续？', '系统提示', {
                confirmButtonText: '导出',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.request({
                    method: "GET",
                    loading:this.$loading(),
                    url: "/api/ProTemporaryWorkHours/Export",
                    responseType: 'blob'
                }).then((res)=>{
                    const blob = new Blob([res])
                    const downloadElement = document.createElement('a');
                    const href = window.URL.createObjectURL(blob); //创建下载的链接
                    downloadElement.href = href;
                    downloadElement.download = '临时工时信息.xls'; //下载后文件名
                    downloadElement.click(); //点击下载
                    window.URL.revokeObjectURL(href); //释放掉blob对象
                })
                

            }).catch((err)=>{
                console.log(err);
            })

        },

        //对话框关闭事件
        closeDialog(){
            this.model={
                WorkhoursType:'', //工时类型
                Workhours:'',     //临时工时
                WorkerName:'',    //员工姓名
                WorkshopName:'',  //所属车间
            }
        },
        search(){
            this.pageDomain.PageIndex=1;
            this.dataBind();
            this.GetWorkhours();
        },
        reset(){
            this.searchForm.WorkhoursType='';
            this.searchForm.Workhours='';
            this.searchForm.WorkerName='';
        },

        //新增或编辑页面回调
        callback(){
            let actionName=this.isUpdate?'编辑':'新增';
            this.showNotify(actionName+'临时工时成功','success');
            this.dialogDomain.dialogVisible=false;
            this.dataBind();
            this.GetWorkhours();
        },
        //分页方法
        handleSizeChange(val){
            this.pageDomain.PageIndex=1;
            this.pageDomain.PageSize=val;
            this.dataBind();
            this.GetWorkhours();
        },
        handleCurrentChange(val){
            this.pageDomain.PageIndex=val;
            this.dataBind();
            this.GetWorkhours();
        },

        //获取数据
        dataBind(){
            this.isLoading=true;
            this.$http.get('/api/ProTemporaryWorkHours',
            {
                params:{
                    PageIndex:this.pageDomain.PageIndex,
                    PageSize:this.pageDomain.PageSize,
                    WorkhoursType:this.searchForm.WorkhoursType,
                    Workhours:this.searchForm.Workhours,
                    WorkerName:this.searchForm.WorkerName,
                }
            })
            .then(res=>{
                this.tableData=res.Data;
                this.pageDomain.TotalCount=res.Count;
                this.isLoading=false;
            })
        },

        //获取总工时数据
        GetWorkhours(){
            this.isLoading=true;
            this.$http.get('/api/ProTemporaryWorkHours/GetWorkhours')
            .then(res=>{
                console.log(res.Data);
                this.WorkhoursSum=res.Data;
                
            })
        },
        //获取数据
        Getxx(){
            this.isLoading=true;
            this.$http.get('/api/PlanTeam/GetAll')
            .then(res=>{
                console.log(res.Data);
                this.WorkhoursSum=res.Data;
                
            })
        }

        

    }

}
</script>

<style scoped>

</style>